var express = require('express');
var router = express.Router();
var retUtil = require('./util/retUtil');
var orderUtil = require('./util/orderUtil');

/*
 * 购物车创建订单并返回支付参数_APP_6_4 添加
 *
 * @author 王俊
 * @date 2018-04-17
 *
 * 参数: device 1    0 表示从pc来的请求
 *                   1 标识一键生成iOS端
 *                   2 标识一键生成android端
 *                   3  标识一键生成 app 嵌入网页
 *                   4 标识一键生成 web 端
 *                   5 标识一键生成微信端
 * 参数: userId	用户唯一标识
 * 参数: couponId 优惠券id, 若没有, 传递 -1
 * 参数: mailType 邮费计算类型, 10 预付邮费, 20 到付邮费, 默认为10, 预支付邮费
 * 参数: addressId	下单地址, 必传
 * 参数: payPlatform	 10 微信支付, 20 支付宝支付, 30 苹果内购
 *
 * 接口地址: http://yapi.yijianxiu.com:7000/project/107/interface/api/2828
 * 测试地址:http://127.0.0.1:300/yjsc/v2/order/createOrderAndPay_app_v6_4
 * */
function createOrderAndPay_app_v6_4(req, res, next) {
    var device = retUtil.numberFromReq(req, 'device');
    var userId = retUtil.numberFromReq(req, 'userId');
    var couponId = retUtil.numberFromReq(req, 'couponId') || 0;
    var mailType = retUtil.numberFromReq(req, 'mailType') || 10;
    var addressId = retUtil.numberFromReq(req, 'addressId') || -1;
    var payPlatform = retUtil.numberFromReq(req, 'payPlatform');
    if(device <=0|| userId<=0){
        return retUtil.ret_err_json("参数有误",res);
    }
    couponId = couponId <= 0 ? 0 : couponId;
    addressId = addressId <= 0 ? 0: addressId;

    var opt = {
        device : device,
        userId : userId,
        couponId : couponId,
        mailType : mailType,
        addrId : addressId,
        payPlatform : payPlatform
    };

    orderUtil.createOrderAndPay_app_v6_4(opt, function (error, data) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(data, res);
    });
}
router.get('/createOrderAndPay_app_v6_4', createOrderAndPay_app_v6_4);
router.post('/createOrderAndPay_app_v6_4', createOrderAndPay_app_v6_4);



/*
 * 创建 vip 商品订单并返回支付参数
 *
 * @author 王俊
 * @date 2018-04-17
 *
 * 参数: device 1    0 表示从pc来的请求
 *                   1 标识一键生成iOS端
 *                   2 标识一键生成android端
 *                   3  标识一键生成 app 嵌入网页
 *                   4 标识一键生成 web 端
 *                   5 标识一键生成微信端
 * 参数: userId	用户唯一标识
 * 参数: itemId 商品唯一标识
 * 参数: couponId -1 标记不使用优惠券, 若使用传递正常优惠券 id
 * 参数: payPlatform	 10 微信支付, 20 支付宝支付, 30 苹果内购
 *
 * 接口地址: http://yapi.yijianxiu.com:7000/project/107/interface/api/2836
 * 测试地址:http://127.0.0.1:300/yjsc/v2/order/createOrderAndPay_app_v6_4
 * */
function createVipOrderAndPay_app_v6_4(req, res, next) {
    var device = retUtil.numberFromReq(req, 'device');
    var userId = retUtil.numberFromReq(req, 'userId');
    var itemId = retUtil.numberFromReq(req, 'itemId');
    var couponId = retUtil.numberFromReq(req, 'couponId') || 0;
    var payPlatform = retUtil.numberFromReq(req, 'payPlatform');
    couponId = couponId <= 0 ? 0 : couponId;

    if(userId <= 0 || device<0 || itemId<=0) {
        return retUtil.ret_err_json('参数错误', res);
    }
    // 创建订单
    orderUtil.createVipMemberOrder_v6_4(device, userId, itemId, couponId,payPlatform, function(crtErr, crtOrderInfo){
        if(crtErr) {
            return retUtil.ret_err_json(crtErr, res);
        }
        return retUtil.ret_succ_json(crtOrderInfo,res);
    });
}
router.get('/createVipOrderAndPay_app_v6_4', createVipOrderAndPay_app_v6_4);
router.post('/createVipOrderAndPay_app_v6_4', createVipOrderAndPay_app_v6_4);


/*
* 获取订单的支付参数
*
* @author 王俊
* @date 2018-04-18
*
* 参数: device:      0 表示从pc来的请求
*                    1 标识一键生成iOS端
*                    2 标识一键生成android端
*                    3  标识一键生成 app 嵌入网页
*                    4 标识一键生成 web 端
*                    5 标识一键生成微信端
* 参数: userId:       用户唯一标识
* 参数: orderId:      订单唯一标识
* 参数: orderType:    商品订单: 10   发票订单: 20
*
* */
function getOrderPayParam_app_v6_4(req, res, next) {
    var device =  retUtil.numberFromReq(req, 'device');
    var userId =  retUtil.numberFromReq(req, 'userId');
    var orderId =  retUtil.numberFromReq(req, 'orderId');
    var orderType =  retUtil.numberFromReq(req, 'orderType');
    if(device<0 ||userId<=0||orderId<=0||orderType<=0) {
        return  retUtil.ret_err_json("参数有误",res);
    }
    var opt = {
        userId : userId,
        orderId : orderId,
        orderType : orderType,
        device : device
    };
    orderUtil.getOderPaymentParams(opt, function (error, params) {
        if(error) {
            return retUtil.ret_err_json(error, res);
        }
        return retUtil.ret_succ_json(params, res)
    });
}
router.get('/getOrderPayParam_app_v6_4', getOrderPayParam_app_v6_4);
router.post('/getOrderPayParam_app_v6_4', getOrderPayParam_app_v6_4);



/**
 * 修改发票订单_APP_6_4 添加
 *
 * 参数: device	 0表示从pc来的请求/1表示ios/2表示android/101表示pc美化大师/102表示pc wps/103表示wps android/104表示wps ios/3 标示app 嵌入网页设备唯一标识
 * 参数: userId	 用户的唯一标识
 * 参数: orderId	发票订单 id
 * 参数: info	"发票信息
 *       {
 *              "invoiceType" :1,  // 1表示公司，2表示个人
 *              "title" : "发票抬头",
 *              "content":"印刷费",
 *              "taxpayerId" : “1111", // 纳税人识别号
 *              "bank" : "开户行",
 *              "bankAccount" : "对公账户号",
 *              "address" : "发票地址",
 *              "mobile" : "电话"
 *       }
 * 参数: payPlatform 10 微信支付, 20 支付宝支付
 * 返回：{
 *            "success":1,
 *            "msg":"成功",
 *            "data":{
 *
 *             }
 *       }
 * 接口地址: http://yapi.yijianxiu.com:7000/project/107/interface/api/2852
 *  测试地址：http://localhost:3000/print/updateInvoiceOrder_app_v6_4?device=1&userId=13&orderId=5&info={%22invoiceType%22:1,%22title%22:%22%E5%8F%91%E7%A5%A8%E6%8A%AC%E5%A4%B4%22,%22content%22:%22%E5%8D%B0%E5%88%B7%E8%B4%B9%22,%22taxpayerId%22:%221111%22,%22bank%22:%22%E5%BC%80%E6%88%B7%E8%A1%8C%22,%22bankAccount%22:%22%E5%AF%B9%E5%85%AC%E8%B4%A6%E6%88%B7%E5%8F%B7%22,%22address%22:%22%E5%8F%91%E7%A5%A8%E5%9C%B0%E5%9D%80%22,%22mobile%22:%22%E7%94%B5%E8%AF%9D%22}
 * */
function updateInvoiceOrder_app_v6_4(req, res, next) {
    var device =  retUtil.numberFromReq(req, 'device');
    var userId =  retUtil.numberFromReq(req, 'userId');
    var orderId =  retUtil.numberFromReq(req, 'orderId');
    var orderType =  retUtil.numberFromReq(req, 'orderType');
    var info =  retUtil.stringFromReq(req, 'info');
    var payPlatform =  retUtil.numberFromReq(req, 'payPlatform');
    if(device<=0 ||userId<=0||orderId<=0||orderType<=0) {
        return  retUtil.ret_err_json("参数有误",res);
    }
    if(info) {
        info = JSON.parse(info);
    }else{
        return retUtil.ret_err_json("info参数有误",res);
    }

    // 更新发票订单
    orderUtil.modifyInvoiceOrder(orderId,info,payPlatform,function(error,invId){
        if(error){
            console.log(error);
            return  retUtil.ret_err_html(error,res);
        }
        return retUtil.ret_succ_json({invId:invId},res);
    });
}
router.get('/updateInvoiceOrder_app_v6_4', updateInvoiceOrder_app_v6_4);
router.post('/updateInvoiceOrder_app_v6_4', updateInvoiceOrder_app_v6_4);



module.exports = router;